package com.example.nbb.Service;

import com.example.nbb.Dao.CollectionDao;
import com.example.nbb.Entity.ResultEntity;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class CollectionService {

    private static final Logger logger = LoggerFactory.getLogger(CollectionService.class);//日志。getLogger需要传入当前的类名

    @Resource
    CollectionDao collectionDao;

    /**
     * 获取所有的收藏
     *
     * @param userId 用户id
     * @return 返回
     */
    public String selectAll(int userId) {
        try {
            return ResultEntity.success(collectionDao.selectAll(userId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 添加收藏
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return 返回
     */
    public String addCollection(int userId, int productId) {
        try {
            return ResultEntity.success(collectionDao.addCollection(userId, productId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 删除收藏接口
     *
     * @param collectionId 收藏id
     * @return 返回
     */
    public String deleteCollection(int collectionId) {
        try {
            return ResultEntity.success(collectionDao.deleteCollection(collectionId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
}
